<?php
/**
 * @link https://www.kancloud.cn/cleverstone/ymb2
 * @copyright Copyright (c) 2020 Yii Manager Software LLC
 */

namespace builder\form\util\control;

use builder\helper\Url;

/**
 * 标签控件
 * @author cleverstone
 * @since ym2.0
 */
class Tag extends Control
{
    /**
     * @var array
     */
    private $_options = [
        'control' => 'tag',
        'label' => '',
        'default' => [],
        'required' => false,
        'comment' => '',
        'help' => '',
        'layout' => 12,
        'style' => '',
        'class' => '',
        'attribute' => '',
        'config' => [
            'allowAdd' => 1,
            'allowClose' => 1,
            'minCount' => 0,
            'maxCount' => 100,
            'remote' => '',
        ],
        //remote开启时生效。
        'modal' => [
            'title' => ' ',
            'titleCloseBtn' => 1,//是否显示标题栏关闭按钮，默认显示
            'titleMaxminBtn' => false,//是否允许全屏最小化，默认否
            'width' => '920px',//指定Modal的宽;
            'height' => '620px',//指定Modal的高;
            'textField' => 'name',//文本字段
            'route' => '',//Modal路由
            'callId' => null,//调用ID
            'closeOnSelected' => false,//选择后是否关闭弹窗
            'refererWindow' => 'self',//模态框`iframe`的来源窗口，默认当前窗口。
        ],
    ];

    /**
     * @param string $context 模态框`iframe`的来源窗口
     * - self
     * - parent
     * - parent.parent.***
     * @return $this
     */
    public function refererWindow($context)
    {
        $this->_options['modal']['refererWindow'] = $context;
        return $this;
    }

    /**
     * 显示标题栏全屏最小化按钮
     * @return $this
     */
    public function titleMaxminBtnDisplay()
    {
        $this->_options['modal']['titleMaxminBtn'] = true;
        return $this;
    }

    /**
     * 隐藏标题栏关闭按钮
     * @return $this
     */
    public function titleCloseBtnHidden()
    {
        $this->_options['modal']['titleCloseBtn'] = 0;
        return $this;
    }

    /**
     * @param string $title modal标题
     * ```php
     * // 文本标题
     * $this->title('编辑');
     *
     * // 支持HTML
     * $this->title(H5::text('编辑'));
     *
     * ```
     *
     * @return $this
     */
    public function modalTitle($title)
    {
        $this->_options['modal']['title'] = (string)$title;
        return $this;
    }

    /**
     * 模态框的宽
     * @param string $width
     * @return $this
     */
    public function modalWidth($width = '100%')
    {
        if (is_numeric($width)) {
            $width .= 'px';
        }

        $this->_options['modal']['width'] = $width;
        return $this;
    }

    /**
     * 模态框的高
     * @param string $height
     * @return $this
     */
    public function modalHeight($height = '100%')
    {
        if (is_numeric($height)) {
            $height .= 'px';
        }

        $this->_options['modal']['height'] = $height;
        return $this;
    }

    /**
     * 文本字段(模态框选中的数据中的用于展示tag的字段)
     * @param string $field 字段名
     * @return $this
     */
    public function modalTextField($field)
    {
        $this->_options['modal']['textField'] = (string)$field;
        return $this;
    }

    /**
     * 模态框路由
     * @param string|array $route
     * @return $this
     */
    public function modalRoute($route)
    {
        $this->_options['modal']['route'] = Url::to($route, '');
        return $this;
    }

    /**
     * 如果Modal中的视图是由ym构建器构建，则callId为视图构建器实例时，定义的ID：
     *
     * example code:
     *
     * ```code
     * // 表格
     * TableBuilder::instance(['id' => 'User_Defined_Table_Id'])
     * // 表单控件
     * $formControl->callId('User_Defined_Table_Id')
     *
     * ```
     * @param string $callId 调用ID
     * @return $this
     */
    public function modalCallId($callId)
    {
        $this->_options['modal']['callId'] = $callId;
        return $this;
    }

    /**
     * 选择后是否关闭弹窗
     * @return $this
     */
    public function modalCloseOnSelected()
    {
        $this->_options['modal']['closeOnSelected'] = true;
        return $this;
    }

    /**
     * 允许远程
     * @return $this
     */
    public function remote()
    {
        $this->_options['config']['remote'] = 1;
        return $this;
    }

    /**
     * @param int $min 允许最小数量
     * @return $this
     */
    public function minCount($min)
    {
        $this->_options['config']['minCount'] = (int)$min;
        return $this;
    }

    /**
     * @param int $max 允许最大数量
     * @return $this
     */
    public function maxCount($max)
    {
        $this->_options['config']['maxCount'] = (int)$max;
        return $this;
    }

    /**
     * 不允许新增
     * @return $this
     */
    public function disallowAdd()
    {
        $this->_options['config']['allowAdd'] = '';
        return $this;
    }

    /**
     * 不允许关闭
     * @return $this
     */
    public function disallowClose()
    {
        $this->_options['config']['allowClose'] = '';
        return $this;
    }

    /**
     * 控件标签
     * @param string|null $label
     * @return $this
     */
    public function label($label)
    {
        $this->_options['label'] = $label;
        return $this;
    }

    /**
     * 标签默认值
     * @param string|array $defaultValue
     * @return $this
     */
    public function defaultValue($defaultValue)
    {
        $this->_options['default'] = (array)$defaultValue;
        return $this;
    }

    /**
     * 是否必填
     * @param bool $required
     * @return $this
     */
    public function required($required = true)
    {
        $this->_options['required'] = (boolean)$required;
        return $this;
    }

    /**
     * 字段注释
     * @param string $comment
     * @return $this
     */
    public function comment($comment)
    {
        $this->_options['comment'] = (string)$comment;
        return $this;
    }

    /**
     * 帮助文字
     * @param string $help
     * @return $this
     */
    public function help($help)
    {
        $this->_options['help'] = (string)$help;
        return $this;
    }

    /**
     * 布局，1-12
     * @param int $layout
     * @return $this
     */
    public function layout($layout)
    {
        $this->_options['layout'] = (int)$layout;
        return $this;
    }

    /**
     * CSS样式
     * @param array|string $style
     * @return $this
     */
    public function style($style)
    {
        $this->formatStyle($style);
        $this->_options['style'] = $style;

        return $this;
    }

    /**
     * UI
     * @param array|string $class
     * @return $this
     */
    public function uiClass($class)
    {
        $this->formatUiClass($class);
        $this->_options['class'] = $class;

        return $this;
    }

    /**
     * HTML属性
     * @param array|string $attribute
     * @return $this
     */
    public function attribute($attribute)
    {
        $this->formatHtmlAttribute($attribute);
        $this->_options['attribute'] = $attribute;

        return $this;
    }

    /**
     * @return array
     */
    public function _toArray()
    {
        return $this->_options;
    }
}